Ext.onReady(function(){
    Ext.define('json_data',{
        extend: 'Ext.data.Model',
        fields: [
			 {name:'id',type:'string'},
			 {name:'month',type:'string'},
			 {name:'employe_id',type:'string'},
			 {name:'employe__name',type:'string'},
			 {name:'employe__department__name',type:'string'},
			 {name:'employe__office__name',type:'string'},
			 {name:'salarylevel__base_salary',type:'string'},
			 {name:'salarylevel__performance_salary',type:'string'},
			 {name:'salarylevel__secrecy_salary',type:'string'},
			 {name:'salarylevel__traffic_subsidy',type:'string'},
			 {name:'salarylevel__comm_subsidy',type:'string'},
			 {name:'yearfee',type:'string'},
			 {name:'monthfee',type:'string'},
			 {name:'dayfee',type:'string'},
			 {name:'tc_fee',type:'string'},
			 {name:'tc_all_fee',type:'string'},
			 {name:'course_fee',type:'string'},
			 {name:'course_all_fee',type:'string'},
			 {name:'trusteeship_fee',type:'string'},
			 {name:'call_fee',type:'string'},
			 {name:'insure_fee',type:'string'},
			 {name:'performance_no',type:'string'},
			 
			 
			 {name:'state',type:'string'},
			 {name:'remark',type:'string'},
			 // 业绩明细
			 {name:'trainingcontract_id',type:'string'},
			 {name:'trainingcontract__name',type:'string'},
			 {name:'course_id',type:'string'},
			 {name:'course__name',type:'string'},
			 {name:'e1',type:'string'},
			 {name:'e2',type:'string'},
			 {name:'rebate',type:'string'},
			 {name:'self_ratio',type:'string'},
			 {name:'fee',type:'string'},
			 {name:'month',type:'string'},
			 {name:'remark',type:'string'},
			 {name:'trainingcontract__contract_type',type:'string'},
			 {name:'real_e',type:'string'},
			 {name:'rebate_fee',type:'string'},
			 {name:'study_type',type:'string'},
			 // 课时明细
			 {name:'company_id',type:'string'},
			 {name:'company__cn_name',type:'string'},
			 {name:'student_id',type:'string'},
			 {name:'student__name',type:'string'},
			 {name:'grade_id',type:'string'},
			 {name:'grade__name',type:'string'},
			 {name:'employe_id',type:'string'},
			 {name:'employe__name',type:'string'},
			 {name:'do_time',type:'string'},
			 {name:'begin_time',type:'string'},
			 {name:'end_time',type:'string'},
			 {name:'course_id',type:'string'},
			 {name:'course__name',type:'string'},
			 {name:'study_type',type:'string'},
			 {name:'course_times',type:'string'},
			 {name:'student_manager_id',type:'string'},
			 {name:'student_manager__name',type:'string'},
			 {name:'student_manager_fee',type:'string'},
			 {name:'price',type:'string'},
			 {name:'ratio',type:'string'},
			 {name:'fee',type:'string'},
			 {name:'month',type:'string'},
			 {name:'remark',type:'string'},
			 // 托管明细 
			 {name:'employe_id',type:'string'},
			 {name:'employe__name',type:'string'},
			 {name:'student_id',type:'string'},
			 {name:'student__name',type:'string'},
			 {name:'grade_id',type:'string'},
			 {name:'grade__name',type:'string'},
			 {name:'course_id',type:'string'},
			 {name:'course__name',type:'string'},
			 {name:'days',type:'string'},
			 {name:'fee',type:'string'},
			 {name:'month',type:'string'},
			 {name:'remark',type:'string'},
			 // 学管师-主管明细
			 {name:'employe_id',type:'string'},
			 {name:'employe__name',type:'string'},
			 {name:'expect_course',type:'string'},
			 {name:'real_course',type:'string'},
			 {name:'real_course_price',type:'string'},
			 {name:'ratio',type:'string'},
			 {name:'real_fee',type:'string'},
			 {name:'student_id',type:'string'},
			 {name:'student__name',type:'string'},
			 {name:'grade_id',type:'string'},
			 {name:'grade__name',type:'string'},
			 {name:'course_id',type:'string'},
			 {name:'course__name',type:'string'},
			 {name:'course_times',type:'string'},
			 {name:'price',type:'string'},
			 {name:'fee',type:'string'},
			 {name:'month',type:'string'},
			 {name:'remark',type:'string'}
        ]
    });
    //创建数据源
    var store = Ext.create('Ext.data.Store', {
    	pageSize:100000,
        model: 'json_data',
	    proxy: {
	        type: 'ajax',
	        url: '/financecontrol/salarymain_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    groupField:'month',
	    autoLoad: true
    });
    //创建数据源
    var storeDetail = Ext.create('Ext.data.Store', {
    	pageSize:100000,
        model: 'json_data',
	    proxy: {
	        type: 'ajax',
	        url: '/financecontrol/salarytcdetail_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    groupField:'month',
	    autoLoad: true
    });
    //创建数据源
    var storeCourseDetail = Ext.create('Ext.data.Store', {
    	pageSize:100000,
        model: 'json_data',
	    proxy: {
	        type: 'ajax',
	        url: '/financecontrol/salarycoursetimesdetail_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    groupField:'month',
	    autoLoad: true
    });
    //创建数据源
    var storeTrusteeshipDetail = Ext.create('Ext.data.Store', {
    	pageSize:100000,
        model: 'json_data',
	    proxy: {
	        type: 'ajax',
	        url: '/financecontrol/salarytrusteeshipdetail_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    groupField:'month',
	    autoLoad: true
    });
    //创建数据源
    var storeCourseManagerDetail = Ext.create('Ext.data.Store', {
    	pageSize:100000,
        model: 'json_data',
	    proxy: {
	        type: 'ajax',
	        url: '/financecontrol/salarycoursetimesmanagerdetail_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    groupField:'month',
	    autoLoad: true
    });
    
    //校区combox
    var companyComboboxForm= Ext.create('Ext.form.field.ComboBox', {
        id:'company_id',
	   	name:'company_id',
       	fieldLabel: '校区', 
       	allowBlank : false,
       	editable:false,
       	emptyText: '请选择',
       	store: new Ext.data.Store({ 
           	singleton : true, 
	   	    proxy: {
	   	        type: 'ajax',
	   	        url: '/resourcecontrol/company_select_all/',
	   	        actionMethods:'post',
	   	        reader: {
	   	            type: 'json'
	   	        }
	   	    },
           fields:['id', 'cn_name'], 
           autoLoad:true 
       	}), 
       	loadingText:'正在加载数据，请稍侯……', 
       	triggerAction:'all', 
       	valueField:'id', 
       	displayField:'cn_name'
    });
    
    //部门combox
    var departmentComboBox= Ext.create('Ext.form.field.ComboBox', { 
       id:'department_id',
	   name:'department_id',
	   allowBlank : false,
       fieldLabel: '部门', 
       editable:false,
       store: new Ext.data.Store({ 
           singleton : true, 
	   	   proxy: {
	   	        type: 'ajax',
	   	        url: '/resourcecontrol/department_select/',
	   	        actionMethods:'post',
	   	        reader: {
	   	            type: 'json'
	   	        }
	   	    },
           fields:['id', 'name'],
           autoLoad:true 
       }), 
       loadingText:'正在加载数据，请稍侯……', 
       triggerAction:'all', 
       valueField:'id', 
       displayField:'name'
    });
    
    //职务combox
    var officeComboBox= Ext.create('Ext.form.field.ComboBox', { 
    	id:'office_id',
    	name:'office_id',
    	allowBlank : false,
    	fieldLabel: '职务', 
    	editable:false,
    	store: new Ext.data.Store({ 
    		singleton : true, 
    		proxy: {
    		type: 'ajax',
    		url: '/resourcecontrol/office_select/',
    		actionMethods:'post',
    		reader: {
    		type: 'json'
    	}
    	},
    	fields:['id', 'name'],
    	autoLoad:true 
    	}), 
    	loadingText:'正在加载数据，请稍侯……', 
    	triggerAction:'all', 
    	valueField:'id', 
    	displayField:'name'
    }); 
    
    
    //多选
    var selModel = Ext.create('Ext.selection.CheckboxModel');
    //创建Grid
    var grid = Ext.create('Ext.grid.Panel',{
        store: store,
        //height: 500,
        //selModel:selModel,
        // 
        features: [{
            id: 'group',
            ftype: 'groupingsummary',
            groupHeaderTpl: '{name}',
            hideGroupedHeader: true,
            enableGroupingMenu: false
        }],
        columnLines:true,
        columns: [
            Ext.create('Ext.grid.RowNumberer'),// 序列
			{text: "ID", width: 80, dataIndex: 'id', sortable: false,hidden : true},
			{text: "月份", width: 80, dataIndex: 'month', sortable: false,renderer: Ext.util.Format.dateRenderer('Y-m')},
            {
            	text: '员工信息',
	            columns: [
	            	{text: "员工ID", width: 80, dataIndex: 'employe_id', sortable: false,hidden:true},
	            	{text: "员工", width: 60, dataIndex: 'employe__name', sortable: false},
	            	{text: "部门", width: 80, dataIndex: 'employe__department__name', sortable: false},
	            	{text: "职位", width: 80, dataIndex: 'employe__office__name', sortable: false}
	            ]
            },
            {
            	text: '基础工资',
	            columns: [
	            	{text: "基本工资", width: 60, dataIndex: 'salarylevel__base_salary' 
	            		 // 列汇总
//	            			,
//						 summaryType: function(records){
//			                var total = 0;
//			                for (var i=0; i < records.length; ++i) {
//			                    record = records[i];
//			                    total += parseInt(record.get('salarylevel__base_salary')) ;
//			                }
//			                return total;
//			            }
	            		//summaryRenderer: Ext.util.Format.usMoney
					},
            		{text: "绩效工资", width: 60, dataIndex: 'salarylevel__performance_salary', sortable: false,hidden:true},
            		{text: "绩效分数", width: 60, dataIndex: 'performance_no', sortable: false,hidden:true},
            		{text: "绩效工资", width: 80, dataIndex: 'real_salarylevel__performance_salary', sortable: false,
	            		renderer: function(value, metaData, record, rowIdx, colIdx, store, view) {
//		                    return Ext.util.Format.usMoney(
//		                    	parseFloat(record.get('salarylevel__performance_salary'))*parseFloat(record.get('performance_no'))
//		                    );
	            			return parseFloat(record.get('salarylevel__performance_salary'))*parseFloat(record.get('performance_no'))
	                	}
            		},
            		{text: "保密工资", width: 60, dataIndex: 'salarylevel__secrecy_salary', sortable: false}
	            ]
            },
            {
            	text: '福利工资',
            	columns: [
            		{text: "交通补贴", width: 60, dataIndex: 'salarylevel__traffic_subsidy', sortable: false},
		            {text: "通讯补贴", width: 60, dataIndex: 'salarylevel__comm_subsidy', sortable: false},
            		{text: "工龄工资", width: 60, dataIndex: 'yearfee', sortable: false},
		            {text: "全勤奖", width: 60, dataIndex: 'monthfee', sortable: false},
		            //{text: "加班费", width: 60, dataIndex: 'workovertimefee', sortable: false},
		            //{text: "其它", width: 60, dataIndex: 'otherfee', sortable: false}
		            //{text: "生日福利", width: 80, dataIndex: 'dayfee', sortable: false}
            	]
            },
            {text: "基础工资合计", width: 80, dataIndex: 'baseTotal', sortable: false,
            	renderer: function(value, metaData, record, rowIdx, colIdx, store, view) {
            			return parseFloat(record.get('salarylevel__base_salary'))+
						parseFloat(record.get('salarylevel__performance_salary'))*parseFloat(record.get('performance_no'))+
                    	parseFloat(record.get('salarylevel__secrecy_salary'))+
                    	
                    	parseFloat(record.get('salarylevel__traffic_subsidy'))+
                    	parseFloat(record.get('salarylevel__comm_subsidy')) +
                    	parseFloat(record.get('yearfee'))+parseFloat(record.get('monthfee')) 
                }
            },
            {
            	text: '业绩提成',
            	columns: [
            		 {text: "合同提成", width: 60, dataIndex: 'tc_fee', sortable: false,
            		 	summaryType: function(records){
			                var total = 0;
			                for (var i=0; i < records.length; ++i) {
			                    record = records[i];
			                    total += parseInt(record.get('tc_fee')) ;
			                }
			                return total.toFixed(2);
			            }
            		 },
            		 {text: "课时提成", width: 60, dataIndex: 'course_fee', sortable: false,
            		 	summaryType: function(records){
			                var total = 0;
			                for (var i=0; i < records.length; ++i) {
			                    record = records[i];
			                    total += parseInt(record.get('course_fee')) ;
			                }
			                return total.toFixed(2);
			            }
            		 },
            		 {text: "托管提成", width: 60, dataIndex: 'trusteeship_fee', sortable: false,
            		 	summaryType: function(records){
			                var total = 0;
			                for (var i=0; i < records.length; ++i) {
			                    record = records[i];
			                    total += parseInt(record.get('trusteeship_fee')) ;
			                }
			                return total.toFixed(2);
			            }
            		 },
            		 {text: "邀约提成", width: 60, dataIndex: 'call_fee', sortable: false,
            		 	summaryType: function(records){
			                var total = 0;
			                for (var i=0; i < records.length; ++i) {
			                    record = records[i];
			                    total += parseInt(record.get('call_fee')) ;
			                }
			                return total.toFixed(2);
			            }
            		 },
            		 {text: "个人提成", width: 60, dataIndex: 'tc_fee', sortable: false,
		            	renderer: function(value, metaData, record, rowIdx, colIdx, store, view) {
		                    return parseFloat(record.get('tc_fee'))+parseFloat(record.get('course_fee')) + 
		                    	parseFloat(record.get('trusteeship_fee')) + parseFloat(record.get('call_fee')) 
		                   
		                },
		                summaryType: function(records){
			                var total = 0;
			                for (var i=0; i < records.length; ++i) {
			                    record = records[i];
			                    total += parseFloat(record.get('tc_fee'))+parseFloat(record.get('course_fee')) + 
		                    	parseFloat(record.get('trusteeship_fee')) + parseFloat(record.get('call_fee')) 
			                }
			                return total.toFixed(2);
			            }
		                },
            		 {text: "团队提成", width: 60, dataIndex: 'tc_all_fee', sortable: false,
		            	renderer: function(value, metaData, record, rowIdx, colIdx, store, view) {
		                    return parseFloat(record.get('tc_all_fee'))
		                }}
            	]
            },
            {text: "保险及个税", width: 80, dataIndex: 'insure_fee', sortable: false},
            {text: "合计", width: 60, dataIndex: 'totalfee', sortable: false,
            	renderer: function(value, metaData, record, rowIdx, colIdx, store, view) {
                    return parseFloat(record.get('salarylevel__base_salary'))+
						parseFloat(record.get('salarylevel__performance_salary'))*parseFloat(record.get('performance_no'))+
                    	parseFloat(record.get('salarylevel__secrecy_salary'))+
                    	parseFloat(record.get('salarylevel__traffic_subsidy'))+
                    	parseFloat(record.get('salarylevel__comm_subsidy')) +
                    	parseFloat(record.get('yearfee'))+parseFloat(record.get('monthfee'))+
                    	
                    	parseFloat(record.get('tc_fee'))+parseFloat(record.get('course_fee')) + 
		                parseFloat(record.get('trusteeship_fee')) + parseFloat(record.get('call_fee'))+
		                
		                parseFloat(record.get('tc_all_fee'))
                },
                summaryType: function(records){
	                var total = 0;
	                for (var i=0; i < records.length; ++i) {
	                    record = records[i];
	                    total += parseFloat(record.get('salarylevel__base_salary'))+
						parseFloat(record.get('salarylevel__performance_salary'))*parseFloat(record.get('performance_no'))+
                    	parseFloat(record.get('salarylevel__secrecy_salary'))+
                    	parseFloat(record.get('salarylevel__traffic_subsidy'))+
                    	parseFloat(record.get('salarylevel__comm_subsidy')) +
                    	parseFloat(record.get('yearfee'))+parseFloat(record.get('monthfee'))+
                    	
                    	parseFloat(record.get('tc_fee'))+parseFloat(record.get('course_fee')) + 
		                parseFloat(record.get('trusteeship_fee')) + parseFloat(record.get('call_fee'))+
		                
		                parseFloat(record.get('tc_all_fee'))
	                }
	                return total.toFixed(2);
	            }
            },
            {text: "状态", width: 60, dataIndex: 'state', sortable: false,
                renderer:function(v){
            	if (v==1) 
                	return '有效'
            	if (v==0) 
            		return '待审核'
            }},
            {text: "备注", width: 150, dataIndex: 'remark', sortable: false}
        ],
        title: '薪资审核',
        // disableSelection: false,
        layout : 'fit',
        //frame:true, 取消边框
        loadMask:true,
        region: 'north',
        //renderTo: 'employee-grid',
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {
    				gridDetail.enable() ;
    				gridCourseDetail.enable() ;
    				gridTrusteeshipDetail.enable() ;
    				gridCourseManagerDetail.enable() ;
                },
                itemdblclick: function(dataview, record, item, index, e) {
                	
                }
            }
        },
        dockedItems : [{
            xtype : 'pagingtoolbar',
            store : store,
            dock : 'bottom',
            displayInfo : true,
            //displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
            autoScroll:true 
        },{
        	dock:'top',
        	xtype:'toolbar',
        	items:[{
        		xtype: 'combobox',
        		id:'company_combox_id',
            	name:'company',
            	labelWidth: 40,
            	width: 140,
               	fieldLabel: '校区',
               	emptyText: '请选择校区',
               	editable:false,
               	store: new Ext.data.Store({ 
                   	singleton : true, 
        	   	    proxy: {
        	   	        type: 'ajax',
        	   	        url: '/resourcecontrol/company_select_all/',
        	   	        actionMethods:'post',
        	   	        reader: {
        	   	            type: 'json'
        	   	        }
        	   	    },
                   fields:['id', 'cn_name'], 
                   autoLoad:true 
               	}), 
               	loadingText:'正在加载数据，请稍侯……', 
               	triggerAction:'all', 
               	valueField:'id', 
               	displayField:'cn_name'        		
        	},{
				xtype:'combobox',
				id:'state',
            	name:'state',
            	labelWidth: 40,
            	width: 140,
				editable:false,
				valueField:'id',
				displayField:'value',
				queryMode:'local',
				fieldLabel : '状态',
				allowBlank : true,
				emptyText : '请选择状态',
				store:Ext.create('Ext.data.Store',{
					fields:['id','value'], 
					data:[
                        {'id':'','value':'全部'},
						{'id':'1','value':'有效'},
						{'id':'0','value':'待审核'}
					]
				})
			},{
        		id:'name',
        		xtype:'textfield',
        		labelWidth: 30,
        		width:130,
        		fieldLabel:'姓名'
        	},{
        		xtype:'button',
        		text:'查询',
        		iconCls:'search', 
        		handler:function(){
	        		if(Ext.getCmp('company_combox_id').getValue()!=null){
	        			//var params = { company_id:Ext.getCmp('company_combox_id').getValue() ,name: Ext.getCmp('name').getValue() };
	        			//if(Ext.getCmp('state').getValue()!=null){
	        				params = { company_id:Ext.getCmp('company_combox_id').getValue() ,name: Ext.getCmp('name').getValue(),state:Ext.getCmp('state').getValue() };
	        			//}
	        		    Ext.apply(store.proxy.extraParams, params);
	        		    store.currentPage=1;
	        		    store.load();
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择校区!');
	    			}
        		}
        	},/*{
        		xtype:'button',
        		text:'添加',
        		iconCls:'add', 
        		handler:function(){
        			form.getForm().reset();
        			win.show();
        		}
        	},{
        		xtype:'button',
        		text:'修改',
        		iconCls:'edit', 
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				form.getForm().reset();
						form.loadRecord(selected[0]);
	        			win.show();
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要修改的员工!');
	    			}
        		}
        	},*/{
        		xtype:'button',
        		text:'审核',
        		iconCls:'edit', 
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				Ext.MessageBox.confirm('提示', '确认审核吗?', function(btn){
	        				if(btn=='yes'){
	        					var ids="";
	                			Ext.each(selected, function (item) {
	                				ids+=item.data.id+",";
	                			});
	                			
	                			Ext.Ajax.request({
	                			    url:'/financecontrol/salarymain_update_state/',
	                			    method : 'post',
	                			    params:{ids:ids,state:'1'},
	                			    success:function(response){
	                			        var results =  Ext.decode(response.responseText);
	                			        if(results.success){
	                			        	store.load();
	                			        }else{
	                			        	Ext.Msg.alert('系统提示', results.message);
	                			        }
	                			    }
	                			});
	                			
	                			
	        				}
	        			});
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要审核的员工!');
	    			}
        			
        		}
        	}/*,{
        		xtype:'button',
        		text:'附件上传',
        		handler:function(){
        			Ext.Msg.alert('系统提示', '此功能正在研发中...');
        		}
        	}*/,{
        		xtype:'button',
        		text:'导出',
        		iconCls:'search', 
        		handler:function(){
        			//params = { company_id:Ext.getCmp('company_combox_id').getValue() ,name: Ext.getCmp('name').getValue(),state:Ext.getCmp('state').getValue() };
	        		if(Ext.getCmp('company_combox_id').getValue()!=null){
	        			window.open('/financecontrol/salarymain_export/?company_id='+Ext.getCmp('company_combox_id').getValue()+
	        					'&name='+Ext.getCmp('name').getValue()+'&state='+Ext.getCmp('state').getValue());
//    					Ext.Ajax.request({
//    					    url:'/financecontrol/salarymain_export/',
//    					    method : 'post',
//    					    params:{
//    					    	company_id:Ext.getCmp('company_combox_id').getValue()
//    					    },
//    					    success:function(response){
//    					        var results =  Ext.decode(response.responseText);
//    					        if(results.success){
//    					        	window.open(results);
//    					        }else{
//    					        	alert(results.message);
//    					        	f.reset();
//    					        	f.login_name.focus();
//    					        }
//    					    }
//    					});	
    					
    					
    					
//	        			var exportForm = Ext.create('Ext.form.Panel', {
//	        		        plain: true,
//	        		        border: 0,
//	        				width: 380,
//	        		        bodyPadding: 5,
//	        		        items: [{
//	        					xtype:'hiddenfield',
//	        					fieldLabel:'company_id',
//	        					name:'company_id',
//	        					value:Ext.getCmp('company_combox_id').getValue()
//	        				}]
//	        			});
//
//	        			var win = Ext.create('Ext.window.Window', {
//	        				modal: true,
//	        				closeAction: 'hide',
//	        				items : [exportForm],
//	        				plain : true,
//	        				resizable : false,
//	        				buttonAlign : 'center',
//	        				buttons : [ {
//	        					text : '保存',
//	        					handler : function() {
//	        						exportForm.getForm().submit({
//        								waitTitle : '请稍候',
//        								waitMsg : '正在处理......',
//        								url : '/financecontrol/salarymain_export/',
//        								success : function(form, action) {
//        									win.hide();
//        								},
//        								failure : function(form, action) {
//        									if (action.result) {
//        										Ext.Msg.alert('系统提示', action.result.message);
//        									} 
//        								}
//        							});
//	        					}
//	        				}, {
//	        					text : '取消',
//	        					handler : function() {
//	        						exportForm.getForm().reset();
//	        						win.hide();
//	        					}
//	        				}]
//	        			}); 	        			
//	        			win.show();
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择校区!');
	    			}
        		}
        	}]
        }]
    });
    

    //创建GridDetail
    var gridDetail = Ext.create('Ext.grid.Panel',{
    	disabled:true,
        store: storeDetail,
        //height: 500,
        //selModel:selModel,
        features: [{
            id: 'group',
            ftype: 'groupingsummary',
            groupHeaderTpl: '{name}',
            hideGroupedHeader: true,
            enableGroupingMenu: false
        }],
        columnLines:true,
        columns: [
            Ext.create('Ext.grid.RowNumberer'),// 序列
			{text: "ID", width: 80, dataIndex: 'id', sortable: false,hidden : true},
			{text: "月份", width: 80, dataIndex: 'month', sortable: false,hidden : true},
			{text: "合同ID", width: 80, dataIndex: 'trainingcontract_id', sortable: false,hidden : true},
            {text: "合同名称", width: 100, dataIndex: 'trainingcontract__name', sortable: false},
            {text: "合同类型", width: 60, dataIndex: 'trainingcontract__contract_type', sortable: false,
            renderer:function(v){
        		if (v==1) 
                	return '新签'
                else if (v==2)
                    return '续费'   
                else if (v==3)
                	return '退费'   
                else if (v==4)
                	return '转介绍'   
            	}
            },
           {text: "课程id", width: 80, dataIndex: 'course_id', sortable: false,hidden : true},
           {text: "课程", width: 100, dataIndex: 'course__name', sortable: false},
           {text: "学习方式", width: 80, dataIndex: 'study_type', sortable: false,
           		renderer:function(v){
           			if (v==1) 
                		return '一对一'  
	                else if(v==2)
	                    return '一对二' 
	                else if(v==3)
	                    return '一对三'    
	                else if(v==4)
	                	return '一对四'    
	    			else if(v==5)
	        			return '一对五'
	        		else if(v==6)
	        			return '户外'
           		}
           	},
           {text: "现金业绩", width: 60, dataIndex: 'e1', sortable: false,
		       	 summaryType: function(records){
		            var total = 0;
		            for (var i=0; i < records.length; ++i) {
		                record = records[i];
		                total += parseFloat(record.get('e1')) ;
		            }
		            return total.toFixed(2);
		        }
           },
           {text: "刷卡业绩", width: 60, dataIndex: 'e2', sortable: false,
           		summaryType: function(records){
		            var total = 0;
		            for (var i=0; i < records.length; ++i) {
		                record = records[i];
		                total += parseFloat(record.get('e2')) ;
		            }
		            return total.toFixed(2);
		        }
           },
           {text: "提成比例%", width: 60, dataIndex: 'self_ratio', sortable: false},
           {text: "真实业绩", width: 60, dataIndex: 'real_e', sortable: false,
           		summaryType: function(records){
		            var total = 0;
		            for (var i=0; i < records.length; ++i) {
		                record = records[i];
		                total += parseFloat(record.get('real_e')) ;
		            }
		            return total.toFixed(2);
		        }
           },
           {text: "合同折扣", width: 60, dataIndex: 'rebate', sortable: false},
           {text: "提成金额", width: 60, dataIndex: 'fee', sortable: false,
           		summaryType: function(records){
		            var total = 0;
		            for (var i=0; i < records.length; ++i) {
		                record = records[i];
		                total += parseFloat(record.get('fee')) ;
		            }
		            return total.toFixed(2);
		        }
           },
           {text: "折后提成", width: 60, dataIndex: 'rebate_fee', sortable: false,
           		summaryType: function(records){
		            var total = 0;
		            for (var i=0; i < records.length; ++i) {
		                record = records[i];
		                total += parseFloat(record.get('rebate_fee')) ;
		            }
		            return total.toFixed(2);
		        }
           },
           {text: "备注", width: 100, dataIndex: 'remark', sortable: false}
        ],
        title: '业绩明细',
        // disableSelection: false,
        layout : 'fit',
        //frame:true, 取消边框
        loadMask:true,
        region: 'north',
        //renderTo: 'employee-grid',
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {
    				
                },
                itemdblclick: function(dataview, record, item, index, e) {
                	
                }
            }
        }
    });
    //创建GridCourseDetail
    var gridCourseDetail = Ext.create('Ext.grid.Panel',{
    	disabled:true,
        store: storeCourseDetail,
        features: [{
            id: 'group',
            ftype: 'groupingsummary',
            groupHeaderTpl: '{name}',
            hideGroupedHeader: true,
            enableGroupingMenu: false
        }],
        //height: 500,
        //selModel:selModel,
        columnLines:true,
        columns: [
            Ext.create('Ext.grid.RowNumberer'),// 序列
			{text: "ID", width: 80, dataIndex: 'id', sortable: false,hidden : true},
			{text: "月份", width: 80, dataIndex: 'month', sortable: false},
			{text: "校区ID", width: 80, dataIndex: 'company_id', sortable: false,hidden : true},
            {text: "校区", width: 60, dataIndex: 'company__cn_name', sortable: false},
            {text: "学生ID", width: 80, dataIndex: 'student_id', sortable: false,hidden : true},
            {text: "学生", width: 60, dataIndex: 'student__name', sortable: false},
            {text: "年级ID", width: 80, dataIndex: 'grade_id', sortable: false,hidden : true},
            {text: "年级", width: 60, dataIndex: 'grade__name', sortable: false},
			{text: "教师ID", width: 80, dataIndex: 'employe_id', sortable: false,hidden : true},
            {text: "教师", width: 60, dataIndex: 'employe__name', sortable: false},
            {text: "日期", width: 80, dataIndex: 'do_time', sortable: false},
            {text: "开始时间", width: 60, dataIndex: 'begin_time', sortable: false},
            {text: "结束时间", width: 60, dataIndex: 'end_time', sortable: false},
           	{text: "课程id", width: 80, dataIndex: 'course_id', sortable: false,hidden : true},
           	{text: "课程", width: 100, dataIndex: 'course__name', sortable: false},
           	{text: "学习方式", width: 80, dataIndex: 'study_type', sortable: false,
           		renderer:function(v){
           			if (v==1) 
                		return '一对一'  
	                else if(v==2)
	                    return '一对二' 
	                else if(v==3)
	                    return '一对三'    
	                else if(v==4)
	                	return '一对四'    
	    			else if(v==5)
	        			return '一对五'
	        		else if(v==6)
	        			return '户外'
           		}
           	},
           	{text: "消耗课时", width: 60, dataIndex: 'course_times', sortable: false//,
//           		summaryType: function(records){
//		            var total = 0;
//		            for (var i=0; i < records.length; ++i) {
//		                record = records[i];
//		                total += record.get('course_times') ;
//		            }
//		            return total.toFixed(2);
//		        }
           	},
           	{text: "学管师ID", width: 60, dataIndex: 'student_manager_id', sortable: false,hidden : true},
           	{text: "学管师", width: 60, dataIndex: 'student_manager__name', sortable: false},
           	{text: "单价", width: 60, dataIndex: 'price', sortable: false,hidden : true},
           	{text: "教师提成比例%", width: 90, dataIndex: 'ratio', sortable: false,hidden : true},
           	{text: "教师提成金额", width: 85, dataIndex: 'fee', sortable: false,hidden : true},
           	{text: "学管师提成金额", width: 100, dataIndex: 'student_manager_fee', sortable: false,hidden : true},
           	{text: "备注", width: 100, dataIndex: 'remark', sortable: false}
        ],
        title: '课时明细',
        // disableSelection: false,
        layout : 'fit',
        //frame:true, 取消边框
        loadMask:true,
        region: 'north',
        //renderTo: 'employee-grid',
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {
    				
                },
                itemdblclick: function(dataview, record, item, index, e) {
                	
                }
            }
        }
    });
    
    
    //创建gridTrusteeshipDetail
    var gridTrusteeshipDetail = Ext.create('Ext.grid.Panel',{
    	disabled:true,
        store: storeTrusteeshipDetail,
        features: [{
            id: 'group',
            ftype: 'groupingsummary',
            groupHeaderTpl: '{name}',
            hideGroupedHeader: true,
            enableGroupingMenu: false
        }],
        //height: 500,
        //selModel:selModel,
        columnLines:true,
        columns: [
            Ext.create('Ext.grid.RowNumberer'),// 序列
			{text: "ID", width: 80, dataIndex: 'id', sortable: false,hidden : true},
			{text: "月份", width: 80, dataIndex: 'month', sortable: false},
			{text: "员工ID", width: 80, dataIndex: 'employe_id', sortable: false,hidden : true},
            {text: "员工", width: 60, dataIndex: 'employe__name', sortable: false},
            {text: "学生ID", width: 60, dataIndex: 'student_id', sortable: false,hidden : true},
            {text: "学生", width: 60, dataIndex: 'student__name', sortable: false},
            {text: "年级ID", width: 60, dataIndex: 'grade_id', sortable: false,hidden : true},
            {text: "年级", width: 60, dataIndex: 'grade__name', sortable: false},
           	{text: "课程id", width: 80, dataIndex: 'course_id', sortable: false,hidden : true},
           	{text: "课程", width: 100, dataIndex: 'course__name', sortable: false},
           	{text: "托管天数", width: 80, dataIndex: 'days', sortable: false},
           	{text: "提成金额", width: 80, dataIndex: 'fee', sortable: false,
           		summaryType: function(records){
		            var total = 0;
		            for (var i=0; i < records.length; ++i) {
		                record = records[i];
		                total += parseFloat(record.get('fee')) ;
		            }
		            return total.toFixed(2);
		        }
           	},
           	{text: "备注", width: 100, dataIndex: 'remark', sortable: false}
        ],
        title: '托管明细',
        // disableSelection: false,
        layout : 'fit',
        //frame:true, 取消边框
        loadMask:true,
        region: 'north',
        //renderTo: 'employee-grid',
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {
    				
                },
                itemdblclick: function(dataview, record, item, index, e) {
                	
                }
            }
        }
    });
    
    
    //创建gridCourseManagerDetail
    var gridCourseManagerDetail = Ext.create('Ext.grid.Panel',{
    	disabled:true,
        store: storeCourseManagerDetail,
        features: [{
            id: 'group',
            ftype: 'groupingsummary',
            groupHeaderTpl: '{name}',
            hideGroupedHeader: true,
            enableGroupingMenu: false
        }],
        //height: 500,
        //selModel:selModel,
        columnLines:true,
        columns: [
            Ext.create('Ext.grid.RowNumberer'),// 序列
			{text: "ID", width: 80, dataIndex: 'id', sortable: false,hidden : true},
			{text: "月份", width: 80, dataIndex: 'month', sortable: false},
			{text: "员工ID", width: 80, dataIndex: 'employe_id', sortable: false,hidden : true},
            {text: "员工", width: 60, dataIndex: 'employe__name', sortable: false},
            //{text: "计划消耗总课时", width: 100, dataIndex: 'expect_course', sortable: false},
            //{text: "实际消耗总课时", width: 100, dataIndex: 'real_course', sortable: false},
            //{text: "实际消耗总课时价格", width: 120, dataIndex: 'real_course_price', sortable: false},
            {text: "提成系数", width: 60, dataIndex: 'ratio', sortable: false},
            //{text: "实际课时总提成", width: 100, dataIndex: 'real_fee', sortable: false},
            {text: "学生ID", width: 60, dataIndex: 'student_id', sortable: false,hidden : true},
            {text: "学生", width: 60, dataIndex: 'student__name', sortable: false},
            {text: "年级ID", width: 60, dataIndex: 'grade_id', sortable: false,hidden : true},
            {text: "年级", width: 60, dataIndex: 'grade__name', sortable: false},
           	{text: "课程id", width: 80, dataIndex: 'course_id', sortable: false,hidden : true},
           	{text: "课程", width: 60, dataIndex: 'course__name', sortable: false},
           	{text: "课时数量", width: 60, dataIndex: 'course_times', sortable: false},
           	{text: "当前单价", width: 60, dataIndex: 'price', sortable: false},
           	{text: "当前提成", width: 60, dataIndex: 'fee', sortable: false},
           	{text: "备注", width: 100, dataIndex: 'remark', sortable: false}
        ],
        title: '学管师-主管明细',
        // disableSelection: false,
        layout : 'fit',
        //frame:true, 取消边框
        loadMask:true,
        region: 'north',
        //renderTo: 'employee-grid',
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {
    				
                },
                itemdblclick: function(dataview, record, item, index, e) {
                	
                }
            }
        }
    });
    
    
    
	var form = Ext.create('Ext.form.Panel', {
		width: 550,
		//frame: true,
		bodyPadding: 5,
        fieldDefaults: {
            labelAlign: 'right',
            labelWidth: 90,
            msgTarget: 'side'
        },
        layout: 'anchor',
        defaults: {
            anchor: '100%'
        },
		items : [{
				xtype:'hiddenfield',
				fieldLabel:'id',
				name:'id'
			},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '姓名',
				name : 'name',
				allowBlank : false,
				blankText : '请输入姓名'
			}, {
				xtype:'combobox',
				editable:false,
				valueField:'id',
				displayField:'value',
				queryMode:'local',
				fieldLabel : '性别',
				name : 'gender',
				allowBlank : false,
				blankText : '请输入性别',
				store:Ext.create('Ext.data.Store',{
					fields:['id','value'], 
					data:[
						{'id':'1','value':'男'},
						{'id':'2','value':'女'}
					]
				}),
				columnWidth: 1/2
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'datefield',
			items:[ {
				fieldLabel : '入职时间',
				editable:false,
				name : 'employed_data',
				allowBlank : false,
				format: 'Y-m-d',
				blankText : '请输入入职时间'
			}, {
				fieldLabel : '转正时间',
				editable:false,
				format: 'Y-m-d',
				name : 'positive_data',
				allowBlank : true
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '身份证',
				name : 'identity_card',
				allowBlank : false,
				blankText : '请输入身份证'
			}, {
				fieldLabel : '户籍地址',
				name : 'registered_residence',
				allowBlank : true
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'datefield',
			items:[ {
				fieldLabel : '出生日期',
				name : 'birthday',
				editable:false,
				allowBlank : false,
				format: 'Y-m-d',
				blankText : '请输入出生日期'
			}, {
				xtype:'textfield',
				fieldLabel : '手机号码',
				name : 'phone',
				allowBlank : false
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '邮箱地址',
				name : 'email',
				allowBlank : true,
				blankText : '请输入'
			}, {
				fieldLabel : 'QQ号码',
				name : 'qq',
				allowBlank : true
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '学历',
				name : 'education',
				allowBlank : true,
				blankText : '请输入学历'
			}, {
				fieldLabel : '专业',
				name : 'profession',
				allowBlank : true
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ departmentComboBox,officeComboBox ]
		},{
			xtype:'container',
			layout:'hbox',
			//defaultType:'textfield',
			items:[ companyComboboxForm,{
				xtype:'combobox',
				editable:false,
				valueField:'id',
				displayField:'value',
				queryMode:'local',
				fieldLabel : '状态',
				name : 'state',
				allowBlank : false,
				blankText : '请输入',
				store:Ext.create('Ext.data.Store',{
					fields:['id','value'], 
					data:[
						{'id':'1','value':'入职'},
						{'id':'2','value':'转正'},
						{'id':'3','value':'离职'},
						//{'id':'4','value':'全职'},
						{'id':'5','value':'兼职'},
						{'id':'6','value':'招聘'}
					]
				}),
				columnWidth: 1/2
			} ]
		},{
			xtype:'container',
			layout:'hbox',
			//defaultType:'textfield',
			items:[ {
				xtype:'combobox',
				editable:false,
				valueField:'id',
				displayField:'value',
				queryMode:'local',
				fieldLabel : '薪档',
				name : 'salary_grade',
				allowBlank : true,
				blankText : '请输入',
				store:Ext.create('Ext.data.Store',{
					fields:['id','value'], 
					data:[
						{'id':'1','value':'XD(A)'},
						{'id':'2','value':'XD(B)'},
						{'id':'3','value':'XD(C)'},
						{'id':'4','value':'XD(D)'}
					]
				}),
				columnWidth: 1/2
			},{
				xtype:'combobox',
				editable:false,
				valueField:'id',
				displayField:'value',
				queryMode:'local',
				fieldLabel : '等级',
				name : 'position_level',
				allowBlank : true,
				blankText : '请输入',
				store:Ext.create('Ext.data.Store',{
					fields:['id','value'], 
					data:[
						{'id':'1','value':'一级'},
						{'id':'2','value':'二级'},
						{'id':'3','value':'三级'},
						{'id':'4','value':'四级'},
						{'id':'5','value':'五级'}
					]
				}),
				columnWidth: 1/2
			} ]
		},{
			xtype:'textfield',
			fieldLabel:'毕业学校',
			name:'graduate_school'
		},{
			xtype:'textareafield',
			fieldLabel:'从业经历',
			name:'work_experience'
		}]
	});

	var win = Ext.create('Ext.window.Window', {
		//title : '---',
		modal: true,
		closeAction: 'hide',
		items : [form],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [ {
			text : '保存',
			handler : function() {
				if (form.getForm().isValid()) {
					form.getForm().submit({
						waitTitle : '请稍候',
						waitMsg : '正在处理......',
						url : '/employecontrol/employe_save/',
						success : function(form, action) {
							//Ext.Msg.alert('系统提示',  action.result.message);
							//form.getForm().reset();
							win.hide();
							store.load();
						},
						failure : function(form, action) {
							if (action.result) {
								Ext.Msg.alert('系统提示', action.result.message);
							} 
						}
					});
				}
			}
		}, {
			text : '取消',
			handler : function() {
				form.getForm().reset();
				win.hide();
			}
		} ]
	});    
    
	// 适应屏幕
	Ext.create('Ext.container.Viewport',{
		layout:'fit',
		renderTo: 'salaryaudit-grid',
		items:[Ext.create('Ext.tab.Panel',{
			activeTab: 0,
			items:[grid,gridDetail,gridCourseDetail,gridTrusteeshipDetail,gridCourseManagerDetail],
			listeners:{
				'tabchange':function(tabPanel, newCard,oldCard,eOpts){
					if(newCard==gridDetail){
						var selected = grid.getSelectionModel().getSelection();
	        			var id="";
	        			var month="";
	        			Ext.each(selected, function (item) {
	        				id=item.data.employe_id;
	        				month=item.data.month;
	        			});

	        			var params = {employe_id:id, month:month};
	        		    Ext.apply(storeDetail.proxy.extraParams, params);
	        		    storeDetail.load();
					}else if(newCard==gridCourseDetail){
						var selected = grid.getSelectionModel().getSelection();
	        			var id="";
	        			var month="";
	        			Ext.each(selected, function (item) {
	        				id=item.data.employe_id;
	        				month=item.data.month;
	        			});

	        			var params = {employe_id:id, month:month};
	        		    Ext.apply(storeCourseDetail.proxy.extraParams, params);
	        		    storeCourseDetail.load();
					}else if(newCard==gridTrusteeshipDetail){
						var selected = grid.getSelectionModel().getSelection();
	        			var id="";
	        			var month="";
	        			Ext.each(selected, function (item) {
	        				id=item.data.employe_id;
	        				month=item.data.month;
	        			});

	        			var params = {employe_id:id, month:month};
	        		    Ext.apply(storeTrusteeshipDetail.proxy.extraParams, params);
	        		    storeTrusteeshipDetail.load();
					}else if(newCard==gridCourseManagerDetail){
						var selected = grid.getSelectionModel().getSelection();
	        			var id="";
	        			var month="";
	        			Ext.each(selected, function (item) {
	        				id=item.data.employe_id;
	        				month=item.data.month;
	        			});

	        			var params = {employe_id:id, month:month};
	        		    Ext.apply(storeCourseManagerDetail.proxy.extraParams, params);
	        		    storeCourseManagerDetail.load();
					}else{// 主grid
						gridDetail.disable() ;
						gridCourseDetail.disable() ;
						gridTrusteeshipDetail.disable() ;
						gridCourseManagerDetail.disable() ;
					}
				}
			}
		})]
	});
	
});